import {LGXEnums}  from "../canvas/common/sysenum";
import canvasEventListner from "../canvas/icanvasEventListner";
import CanvasEventSvc from './canvasMouseEventSvc.js'
import commandManager from "./commandManager";
import globalConst from "../canvas/common/globalConst";
var global = globalConst.GlobalConst;



class CanvasManager extends canvasEventListner.ICanvasEventListner {
    constructor() {
        super();
        this.m_pCommandMng = new commandManager.CommandManager();
        console.log("CanvasManager-------------initial--------------");
        const currentDate = new Date();
        this.preEnterDownTime = currentDate.getTime();
    }
    actToEvent(evt) {
        CanvasEventSvc.CanvasEventSvc.act2Event(evt);
        let cmdResult = this.m_pCommandMng.notifyMapActionProcess(evt);
        if (cmdResult && window.cmdInputDlg.editMode == "autocad") {
            window.cmdInputDlg.appendNewCmdLine(cmdResult);
        }
    }

    getDefaultCMD = function (precmdIn) {
        let precmd = precmdIn.replace(/（/g, "(");
        precmd = precmd.replace(/）/g, ")");
        let cmd = "";
        if (precmd.endsWith(")")) {
            let prePos = precmd.lastIndexOf("(");
            if (prePos >= 0){
                cmd = precmd.substring(prePos + 1, precmd.length - 1);
            }
            else{
                return "";
            }
        }
        let selItemIdx = cmd.indexOf("/");
        if (selItemIdx >= 0) {
            //带有选择项的，默认选/右边那个
            cmd = cmd.substring(selItemIdx + 1, precmd.length - selItemIdx - 1);
        }
        return cmd;
    }

    actToKeyEvent(event, mainctl) {
        let cmdInputEle = document.getElementById("cmdinpuArea");
        this.forbidDelPreCmd(cmdInputEle,event);
        if (event.srcElement != cmdInputEle) {
            return;
        }

        if (event.code == 'Enter') {
            const currentDate = new Date();
            let curenTime = currentDate.getTime();
            if ((curenTime - this.preEnterDownTime) < 30) {
                return;
            }
            this.preEnterDownTime = currentDate.getTime();
            // console.log("on enter--1");
            let txt = window.cmdInputDlg.cmdInfoArea;
            let cmdLst = txt.split(">");
            if (cmdLst.length > 0) {
                // console.log("on enter--2");
                let curentCmd = cmdLst[cmdLst.length - 1];
                if (curentCmd.length == 0 && cmdLst.length > 1) {
                    let preCmd = cmdLst[cmdLst.length - 2];
                    curentCmd = this.getDefaultCMD(preCmd);
                }
                console.log("curentCmd:" + curentCmd);
                if (!curentCmd || (curentCmd && curentCmd == "")){
                    return 
                }
                else{
                    this.actToCmd(curentCmd.trim(), mainctl);
                }
            }
        }
        else if (event.code == "Escape") {
            this.actToCmd("Esc", mainctl);
            mainctl.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
            let rst = { nextTips: "命令", appendToPreCMD: "Esc" };
            window.cmdInputDlg.appendNewCmdLine(rst, false);

            rst = { nextTips: "", appendToPreCMD: "pick" };
            window.cmdInputDlg.appendNewCmdLine(rst, false);
        }
    }

    actToCmd(cmd, mainctl) {
        if (cmd.length == 0) {
            return;
        }
        let cmdResult = this.m_pCommandMng.act2CMD(cmd, mainctl);
        if (cmdResult && window.cmdInputDlg.editMode == "autocad") {
            cmdResult.appendToPreCMD = "";
            return window.cmdInputDlg.appendNewCmdLine(cmdResult, true);
        }
    }

//     { id: 0X030B0000, name: '矩形', iconURL: '/res/icon/rect1.png', category: "base" },
// { id: 0X030C0000, name: '圆形', iconURL: '/res/icon/circle1.png', category: "base" },
// { id: 0X03230000, name: '椭圆形', iconURL: '/res/icon/ellipse1.png', category: "base" },
// // { id: 0X030D0000, name: '多边形', iconURL: '/res/icon/polygon.png', category: "base" },
// { id: 0X03200000, name: '圆弧', iconURL: '/res/icon/arc.png', category: "base" },
// { id: 0X03140000, name: '椭圆弧', iconURL: '/res/icon/ellipsearc.png', category: "base" },
// { id: 0X03120000, name: '辅助线', iconURL: '/res/icon/auxline.png', category: "base" },
// { id: 0X03130000, name: '样条曲线', iconURL: '/res/icon/spline.png', category: "base" },
    sendCMD(cmd, mainctl) {
        if (cmd == "draw_line") {
            this.setDrawTool(0x030a0003, mainctl);
        }
        else if (cmd == "draw_polyline") {
            this.setDrawTool(0x030a0004, mainctl);
        }
        else if (cmd == "draw_rect") {
            this.setDrawTool(0x030b0000, mainctl);
        }
        else if (cmd == "draw_circle") {
            this.setDrawTool(0x030c0000, mainctl);
        }
        else if (cmd == "draw_ellipse") {
            this.setDrawTool(0x03230000, mainctl);
        }
        else if (cmd == "draw_arc") {
            this.setDrawTool(0x03200000, mainctl);
        }
        else if (cmd == "draw_ellipsearc") {
            this.setDrawTool(0x03140000, mainctl);
        }
        else if (cmd == "draw_spline") {
            this.setDrawTool(0x03130000, mainctl);
        }
        else if (cmd == "draw_auxline") {
            this.setDrawTool(0x03120000, mainctl);
        }
        else {
            let cmdResult = this.m_pCommandMng.sendCMD(cmd, mainctl);
            let t = window.cmdInputDlg.value;
            if (cmdResult && window.cmdInputDlg.editMode == "autocad") {
                window.cmdInputDlg.appendNewCmdLine(cmdResult);
                let ele = document.getElementById("cmdinpuArea");
                if (ele) {
                    ele.focus();
                    ele.addEventListener("keydown", window.keyInputFunction);
                }
            }
        }
    }

    setDrawTool(fullType, mainctl) {
        mainctl.setTool(fullType);
        let cmdResult = this.m_pCommandMng.actToSetDrawTool(fullType, mainctl);
        let t = window.cmdInputDlg;
        if (cmdResult && window.cmdInputDlg.editMode == "autocad") {
            window.cmdInputDlg.appendNewCmdLine(cmdResult);
            let ele = document.getElementById("cmdinpuArea");
            if (ele) {
                ele.focus();
                ele.addEventListener("keydown", window.keyInputFunction);
            }
        }
    }
   

    forbidDelPreCmd(cmdInputEle,event){
        if (event.srcElement != cmdInputEle || event.code != "Backspace") {
            return ;
        }

        if (this.chkDelPreCmd(cmdInputEle)) {
            event.preventDefault();
            event.stopPropagation();
        }
    }

    chkDelPreCmd(cmdInputEle){
        let txt = window.cmdInputDlg.cmdInfoArea;
        let lenth = txt.length;
        let strList = txt.split("");
        const cursorStart = cmdInputEle.selectionStart;
        if (strList.length >= cursorStart && strList[cursorStart-1] == ">"){
            return true;
        }

        for (let idx = cursorStart; idx < strList.length;idx++){
            let ch = strList[idx];
            if (ch == ">"){
                return true;
            }
        }
        const cursorEnd = cmdInputEle.selectionEnd;
        
        console.log('光标起始位置：', cursorStart);
        console.log('光标结束位置：', cursorEnd);
        return false;
    }

    initAccountDatas() {
        let graphPropDef = {
            name: {
                selectFlag: false,
                slider: false
            },
            linewidth: {
                selectFlag: true,
                childitem: [
                    { id: 'linewidth@1', value: '1' },
                    { id: 'linewidth@2', value: '2' },
                    { id: 'linewidth@3', value: '3' },
                    { id: 'linewidth@4', value: '4' }
                ]
            },
            num: {
                selectFlag: false,
                vtype: 'string',
                notnull: true,
                vrepx: 'number',
                slider: false
            },
            xlink: {
                selectFlag: false,
                slider: false
            }

        };

        let tableForm = {
            tableData: [
                {
                    propName: 'linewidth',
                    propLabel: '线宽',
                    propValue: '2'
                },
                {
                    propName: 'name',
                    propLabel: '名称',
                    propValue: '测试'
                },
                {
                    propName: 'num',
                    propLabel: '编号',
                    propValue: "201"
                },
                {
                    propName: 'xlink',
                    propLabel: '链接',
                    propValue: "2ded1063-9f8e-44e8-8c78-51fb52351d3d"
                }
            ]
        };
        return { graphPropDef, tableForm };
    }

}

export default { CanvasManager }